//
//  M_FuliCell.swift
//  letuofu
//
//  Created by WH-JS on 2019/12/27.
//  Copyright © 2019 WH-JS. All rights reserved.
//

import UIKit

class M_FuliCell: QuickTableCellWithInit<[String : Any?]> {
    //定义一个闭包类型
    typealias addBlock = ([String : Any?]) -> Void
    //声明一个变量
    var addBtnBack: addBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func callAddBlock(block: @escaping addBlock) {
        addBtnBack = block
    }
    
    let addBtn = Button()
    let titleLab = UILabel()
    let priceLab = UILabel()
    let subLab = UILabel()
    
    override class func heightOf(_ data:[String:Any?])->CGFloat{
        return 113.5
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override func initCell(container: UIView) {
        container.backgroundColor = .xF2F2F2
        
        drawView { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.right.bottom.equalToSuperview()
        }.backgroundColor = .white
        
        drawLabel(label: titleLab, text: "", fontSize: 14, fontWeight: .medium, textColor: .x333333) { (make) in
            make.top.equalToSuperview().offset(30)
            make.left.equalToSuperview().offset(10)
            make.width.equalTo(150)
        }
        titleLab.numberOfLines = 2
        
        drawLabel(label: priceLab, text: "", fontSize: 18, fontWeight: .regular, textColor: .xFC4C02) { (make) in
            make.top.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-10)
        }
        
        //画横线
        let line = drawView { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalToSuperview().offset(-44)
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        drawLabel(label: subLab, text: "", fontSize: 12, fontWeight: .regular, textColor: .x999999) { (make) in
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-18)
        }
        
        drawButton(button: addBtn,title: "未知状态", titleColor: .white, fontSize: 14) { (make) in
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-10)
            make.height.equalTo(25)
            make.width.equalTo(60)
        }
        addBtn.layer.cornerRadius = 12.5
        addBtn.hitEdgInsets = .init(top: -20, left: -20, bottom: -20, right: -20)
        addBtn.addTarget(self, action: #selector(_addButtonClick(button:)), for: .touchUpInside)
    }
    
    
    override func fillCell(_ data: [String:Any?], at index: Int) {
        titleLab.text = data.getString("welFareName")
        let btime = data.getString("actionBTime")?.prefix(10) ?? "现在"
        let etime = data.getString("actionETime")?.prefix(10) ?? "至今"
        subLab.text = btime.replacingOccurrences(of: "-", with: ".") + "-" + etime.replacingOccurrences(of: "-", with: ".")
//        let cents = data.getInt("cents") ?? 0
        
        priceLab.text = data.getString("cents")
        if data.getString("welfareStatusCode") ==  "120001" {
            addBtn.setTitle("已领取", for: .normal)
            addBtn.backgroundColor = .x999999
        }
        else if data.getString("welfareStatusCode") ==  "120002" {
            addBtn.setTitle("领取", for: .normal)
            addBtn.backgroundColor = .xFC4C02
        }
    }
    
    @objc func _addButtonClick(button: Button) {
        
//        if self.data?.getString("welfareStatusCode") == "120001" {
//            let popup = PopupBox(infoText: "当前福利已经领取")
//            popup.show(vc: UIViewController.topMostController() ?? UIViewController())
//            return
//        }
        if addBtnBack != nil {
            addBtnBack!(self.data!)
        }
    }
}
